#!/bin/sh

HOME_DIR=/home
MUSIC_BASE_DIR=/home/lancerx/data_extra/musics

if [ $# -lt 2 ]; then
	echo "Usage: $0 m3u_file export_dir"
	exit 1
fi

if [ ! -e $1 ]; then
	echo "File \`$1' not exists"
	exit 1
fi

if [ -e $2 ]; then
	echo "Output dir \`$2' already exists"
	exit 1
fi

mkdir "$2" || exit 2

SAVEIFS="${IFS}"
export IFS=$(echo -ne "\n\b")
for f in $(grep "^[^#]" $1) ;
do
	subdir=$(dirname "$HOME_DIR/$f")
	mkdir -p "$2/$subdir"
	cp "$HOME_DIR/$f" "$2/$subdir/"
done

mv $2/$MUSIC_BASE_DIR/* "$2/"
rmdir "$2/$MUSIC_BASE_DIR"

export IFS="${SAVEIFS}"
